package com.itcam.phaseii.generic;

import java.util.ArrayList;

public class Generic02 {
    public static void main(String[] args) {
        //使用传统的方法来解决===>使用泛型

        //1. 当我们ArrayList<Dog>表示存放到ArrayList集合中的元素是Dog类型(细节后面说...)
        //2. 如果编译器发现添加的类型，不满足要求，就会报错
        //3. 在遍历的时候，可以直接取出Dog类型而不是Object
        //4. public class ArrayList<E> {} E称为泛型,那么Dog->E

        ArrayList<Doggie> arrayList = new ArrayList<Doggie>();

        arrayList.add(new Doggie("旺财", 10));
        arrayList.add(new Doggie("发财", 1));
        arrayList.add(new Doggie("小黄", 5));

//        arrayList.add(new Katty("小橘",1)); // 报错

        System.out.println("===使用泛型====");
        for (Doggie doggie : arrayList) {
            System.out.println(doggie.getName() + "——" + doggie.getAge());
        }
    }
}

class Doggie {
    private String name;
    private int age;

    public Doggie(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Katty {
    private String name;
    private Integer age;

    public Katty(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Katty{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
